package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebProductOrderDao;
import ysq.xyg.web.model.po.WebProductOrder;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebProductOrderVo;
import ysq.xyg.web.service.WebProductOrderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-兑换订单service实现
 *
 * @author shengQiang yu
 * @date 2019-7-16 9:15:47
 */
@Service("webProductOrderService")
public class WebProductOrderServiceImpl extends BaseServiceImpl<WebProductOrder> implements WebProductOrderService {

    @Resource
    private WebProductOrderDao webProductOrderDao;

    private WebProductOrder webProductOrder;

    @Autowired
    public void setBaseDao(WebProductOrderDao webProductOrderDao) {
        this.baseDao = webProductOrderDao;
    }

    @Override
    public List<WebProductOrder> listLeft(WebProductOrderVo webProductOrderVo) {
        return webProductOrderDao.listLeft(webProductOrderVo);
    }

    /**
     * 分页查询
     *
     * @param webProductOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:15:47
     */
    public ReturnEntity page(WebProductOrderVo webProductOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebProductOrder> list = listLeft(webProductOrderVo);
        PageInfo<WebProductOrder> page=new PageInfo<WebProductOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webProductOrderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:15:47
     */
    public ReturnEntity addOrUpdate(WebProductOrderVo webProductOrderVo, SysUser currentUser) {
        webProductOrder=selectByPrimaryKey(webProductOrderVo.getId());
        if(ObjectUtil.isNull(webProductOrder)){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        webProductOrder.setUpdateTime(new Date());
        webProductOrder.setUpdateUser(currentUser.getId());
        webProductOrder.setOperateStatus(webProductOrderVo.getOperateStatus());
        webProductOrder.setOperateResult(webProductOrderVo.getOperateResult());
        int num=updateByPrimaryKey(webProductOrder);
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-16 9:15:47
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebProductOrder webProductOrder=selectByPrimaryKey(id);
        if(webProductOrder!=null){
            webProductOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webProductOrder.setUpdateTime(new Date());
            webProductOrder.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webProductOrder);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webProductOrderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-16 9:15:47
     */
    public List<WebProductOrder> acceptList(WebProductOrderVo webProductOrderVo){
        Example example = new Example(WebProductOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webProductOrderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webProductOrderVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webProductOrderDao.selectByExample(example);
    }
}
